package it.enricod.jcontextfree.engine.model.impl;

import it.enricod.jcontextfree.engine.model.FigureEnum;
import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;

import it.enricod.jcontextfree.engine.model.IShape;

public class Shape implements IShape {

    private Logger logger = Logger.getLogger("Shape");
    private final String name;
    private Map<String, Object> attributes = new HashMap<String, Object>();

    public Shape(String name) {
        assert name != null;
        this.name = name.trim();
    }

    public String getName() {
        return name;
    }

    public boolean isPrimitive() {
        FigureEnum[] figures = FigureEnum.values();
        for (int i = 0; i < figures.length; i++) {
            if (name.equals(figures[i].getName())) {
                return true;
            }
        }
        return false;
    }

    public void writeAttribute(String attributeName, Object value) {
        logger.debug(this.name + ", setting attribute " + attributeName + "=" + value);
        attributes.put(attributeName, value);
    }

    public Double getAttributeAsDouble(String attributeName, double defaultValue) {
        Object result = attributes.get(attributeName);
        if (result == null) {
            return defaultValue;
        }
        if (result instanceof Double) {
            return (Double) result;
        } else {
            throw new IllegalArgumentException("parametro " + attributeName + " non e' di tipo Double");
        }
    }
}
